//
//  YCShareCore.m
//  YCVillageDoctor
//
//  Created by itclimb on 2017/7/13.
//  Copyright © 2017年 zhangxiaoliang. All rights reserved.
//

#import "YCShareCore.h"

@interface YCShareCore ()<UIGestureRecognizerDelegate>

@property (nonatomic,copy)NSString *title;
@property (nonatomic,copy)NSString *des;
@property (nonatomic)id thumbimage;
@property (nonatomic,copy)NSString *link;
@property (nonatomic,copy) UMSocialRequestCompletionHandler hander;

@property(nonatomic, strong) YCShareUI *sheetView;

@end

@implementation YCShareCore

+ (instancetype)configWithTitle:(NSString *)title
                            Des:(NSString *)des
                     ThumbImage:(id)thumbimage
                           Link:(NSString *)link
                Completehandler:(UMSocialRequestCompletionHandler)handler
{
    YCShareCore *shareView = [[YCShareCore alloc]initWithFrame:[UIScreen mainScreen].bounds];
    shareView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.8f];
    shareView.title = title;
    shareView.des = des;
    shareView.link = link;
    shareView.thumbimage = thumbimage;
    shareView.hander = handler;
    [shareView createUI];
    return shareView;
}

- (void)createUI{
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    _sheetView = [[YCShareUI alloc] init];
    
    __weak __typeof__(self)weakSelf = self;
    [_sheetView setShareUIDismissBlock:^{
        [weakSelf dismiss];
    }];
    
    [_sheetView setShareUIDidSelectBlock:^(NSIndexPath *indexPath, NSArray *array){
        
        UMSocialPlatformType platformType = UMSocialPlatformType_WechatSession;
        NSDictionary *dict = array[indexPath.row];
        NSString *platformStr = dict[@"platform"];
        
        if ([platformStr isEqualToString:@"UMSocialPlatformType_WechatSession"]) {
            platformType = UMSocialPlatformType_WechatSession;
            
        }else if ([platformStr isEqualToString:@"UMSocialPlatformType_WechatTimeLine"]){
            platformType = UMSocialPlatformType_WechatTimeLine;
            
        }else if ([platformStr isEqualToString:@"UMSocialPlatformType_WechatFavorite"]){
            platformType = UMSocialPlatformType_WechatFavorite;
            
        }else if ([platformStr isEqualToString:@"UMSocialPlatformType_QQ"]){
            platformType = UMSocialPlatformType_QQ;
            
        }else if ([platformStr isEqualToString:@"UMSocialPlatformType_Qzone"]){
            platformType = UMSocialPlatformType_Qzone;
            
        }else if([platformStr isEqualToString:@"UMSocialPlatformType_Sina"]){
            platformType = UMSocialPlatformType_Sina;
        }
        
        UIImage *image;
        if ([weakSelf.thumbimage isKindOfClass:[UIImage class]]) {
            image = weakSelf.thumbimage ;
        }else if([weakSelf.thumbimage isKindOfClass:[NSString class]]){
            image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:weakSelf.thumbimage]]];
        }else if([weakSelf.thumbimage isKindOfClass:[NSURL class]]){
            image = [UIImage imageWithData:[NSData dataWithContentsOfURL:weakSelf.thumbimage]];
        }
        
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        
        //创建网页内容对象
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:weakSelf.title
                                                                                 descr:weakSelf.des
                                                                             thumImage:image];
        //设置网页地址
        shareObject.webpageUrl = weakSelf.link;
        
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
        
        [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(id result, NSError *error) {
            
            if (weakSelf.hander) {
                weakSelf.hander(result, error);
            }
            
            if (error) {
                LJXLog(@"Share fail with error %@",error);
            }else{
                LJXLog(@"response data is %@",result);
            }
        }];
        
    }];
    
    [self addSubview:_sheetView];
    [_sheetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(_sheetView.height);
        make.leading.trailing.equalTo(self);
        make.top.mas_equalTo(self.mas_bottom);
    }];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isEqual:self]) {
        return YES;
    }
    return NO;
}

- (void)setSharePlatform:(YCSharePlatform)sharePlatform{
    _sharePlatform = sharePlatform;
    self.sheetView.sharePlatform = sharePlatform;
}

//MARK: - 弹出分享面板
-(void)show{
    UIWindow *current = [UIApplication sharedApplication].keyWindow;
    [current addSubview:self];
    [UIView animateWithDuration:.35f
                     animations:^{
                         _sheetView.transform = CGAffineTransformMakeTranslation(0, -_sheetView.height);
                     } completion:^(BOOL finished) {
                         
                         
                     }];
}

//MARK: - 退出分享面板
-(void)dismiss{
    [UIView animateWithDuration:.35f
                     animations:^{
                         _sheetView.transform = CGAffineTransformIdentity;
                     } completion:^(BOOL finished) {
                         [self removeFromSuperview];
                     }];
}


@end

